package com.yunhe.option.service.base.impl;

import com.yunhe.option.domain.base.PriceType;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.option.repository.base.PriceTypeRepository;
import com.yunhe.option.service.base.PriceTypeService;
import com.yunhe.common.util.QueryUtil;
import com.yunhe.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 价格类型Service
 * @desc 价格类型服务类实现
 * @author liuronglei
 */
@Service
public class PriceTypeServiceImpl implements PriceTypeService {
    @Autowired
    private PriceTypeRepository priceTypeRepository;

    /**
     * 价格类型对象处理和判断
     * @param priceType 价格类型对象
     */
    private void processBeforeSave(PriceType priceType)
            throws ArgumentErrorException, UniqueConstraintsException {
        //判断价格类型代码、编号、标题是否为空
        if (priceType.getCode() == null
                || StringUtil.isEmpty(priceType.getName())
                || StringUtil.isEmpty(priceType.getTitle())) {
            throw new ArgumentErrorException("价格类型代码、编号、标题不能为空");
        }
        //判断单位代码是否已存在
        PriceType typeByCode = this.findByCode(priceType.getCode());
        if (typeByCode != null) {
            if (priceType.getId() == null || typeByCode.getId().longValue() != priceType.getId().longValue()) {
                throw new UniqueConstraintsException("该价格类型代码已存在");
            }
        }
        //判断单位编号是否已存在
        PriceType typeByName = this.findByName(priceType.getName());
        if (typeByName != null) {
            if (priceType.getId() == null || typeByName.getId().longValue() != priceType.getId().longValue()) {
                throw new UniqueConstraintsException("该价格类型编号已存在");
            }
        }
        //如果序号为空，则默认为代码
        if (priceType.getSn() == null && priceType.getCode() != null) {
            priceType.setSn(priceType.getCode());
        }
    }

    /**
     * 新增价格类型
     * @param priceType 价格类型对象
     */
    @Override
    @Transactional
    public PriceType createPriceType(PriceType priceType)
            throws ArgumentErrorException, UniqueConstraintsException {
        priceType.setId(null);
        processBeforeSave(priceType);
        return priceTypeRepository.save(priceType);
    }

    /**
     * 删除价格类型
     * @param priceTypeId 价格类型ID
     */
    @Override
    @Transactional
    public void deletePriceType(Long priceTypeId) {
        priceTypeRepository.deleteById(priceTypeId);
    }

    /**
     * 更新价格类型（更新全部字段）
     * @param priceType 价格类型对象
     */
    @Override
    @Transactional
    public PriceType updatePriceType(PriceType priceType)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        boolean exists = priceType.getId() != null && priceTypeRepository.existsById(priceType.getId());
        if (!exists) {
            throw new ObjectNotFoundException("找不到ID为" + priceType.getId() + "的价格类型");
        }
        processBeforeSave(priceType);
        return priceTypeRepository.save(priceType);
    }

    /**
     * 更新价格类型（更新指定字段）
     * @param priceTypeId 价格类型ID
     * @param code 代码
     * @param name 编号
     * @param title 标题
     * @param sn 排序序号
     * @param activity 是否可用
     */
    @Override
    @Transactional
    public PriceType updatePriceType(Long priceTypeId,
                                     Long code,
                                     String name,
                                     String title,
                                     Long sn,
                                     Boolean activity)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        PriceType priceType = this.findByPriceTypeId(priceTypeId);
        if (priceType == null) {
            throw new ObjectNotFoundException("找不到ID为" + priceTypeId + "的价格类型");
        }
        Optional.ofNullable(code).ifPresent(priceType::setCode);
        Optional.ofNullable(name).ifPresent(priceType::setName);
        Optional.ofNullable(title).ifPresent(priceType::setTitle);
        Optional.ofNullable(sn).ifPresent(priceType::setSn);
        Optional.ofNullable(activity).ifPresent(priceType::setActivity);
        processBeforeSave(priceType);
        return priceTypeRepository.save(priceType);
    }

    /**
     * 根据ID获得价格类型对象
     * @param id 状态ID
     */
    @Override
    public PriceType findByPriceTypeId(Long id) {
        return priceTypeRepository.findById(id).orElse(null);
    }

    /**
     * 根据代码获得价格类型对象
     * @param code 代码
     */
    @Override
    public PriceType findByCode(Long code) {
        return priceTypeRepository.findByCode(code);
    }

    /**
     * 根据编号获得价格类型对象
     * @param name 编号
     */
    @Override
    public PriceType findByName(String name) {
        return priceTypeRepository.findByName(name);
    }

    /**
     * 获得全部价格类型对象
     */
    @Override
    public Iterable<PriceType> findPriceTypes() {
        return priceTypeRepository.findAll(QueryUtil.defaultSort());
    }

}
